<script setup lang="ts">
import { DiffType } from '../types'
import type { SplitLineChange,Covereds } from '../types'
import { computed, ref, watch } from 'vue-demi'
const props = defineProps<{
  splitLine: SplitLineChange
  covered:Covereds
}>()
//const prop=ref(props);
const emit = defineEmits(['expand'])

function getCodeMarker(type: DiffType) {
  if (type === DiffType.DELETE)
    return '-'
  if (type === DiffType.ADD)
    return '+'
  return ''
}

function onSplitLineMousedown(side: 'left' | 'right') {
  const leftElements = document.querySelectorAll('.file-diff-split .split-side-left')!
  const rightElements = document.querySelectorAll('.file-diff-split .split-side-right')!

  for (const el of rightElements)
    el.classList.toggle('no-select', side === 'left')

  for (const el of leftElements)
    el.classList.toggle('no-select', side === 'right')
}
function getStytle(num:number){
  for(var index in props.covered){
    var line=props.covered[index];
    if(line.line===num){
      return line.background;
    }
  }
}
function isTest(num:number){
  for(var index in props.covered){
    var line=props.covered[index];
    if(line.line===num && line.isTest===1){
      return true;
    }
  }
}
</script>

<template>
  <tr v-if="splitLine.hideIndex !== undefined && splitLine.hide">
    <td class="blob-num blob-num-hunk" colspan="1" @click="emit('expand', splitLine)">
      >
    </td>
    <td class="blob-code blob-code-inner blob-code-hunk" colspan="3" align="left">
      ⋯
    </td>
  </tr>
  <tr v-else-if="!splitLine.hide">
    <template v-for="(line, index) in [splitLine.left, splitLine.right]">
      <!-- eslint-disable -->
      <template v-if="line.type === DiffType.EMPTY">
        <td class="blob-num blob-num-empty empty-cell" />
        <td class="blob-code blob-code-empty empty-cell" />
      </template>
      <template v-else>
        <td
          class="blob-num"
          :class="{
            'blob-num-deletion': line.type === DiffType.DELETE,
            'blob-num-addition': line.type === DiffType.ADD,
            'blob-num-context': line.type === DiffType.EQUAL,
            'blob-num-hunk': splitLine.hide !== undefined,
          }"
        >
          {{ line.num }}
        </td>
        <td
          class="blob-code"
          :style="index === 1?'background-color:'+getStytle(line.num):''"
          :class="{
            'blob-code-deletion': line.type === DiffType.DELETE,
            'blob-code-addition': line.type === DiffType.ADD,
            'blob-code-context': line.type === DiffType.EQUAL,
            'blob-code-hunk': splitLine.hide !== undefined,
            'split-side-left': index === 0,
            'split-side-right': index === 1,
            'circle-border':index === 1 && isTest(line.num)==true,
          }"
          @mousedown="onSplitLineMousedown(index === 0 ? 'left' : 'right')"
        >
          <span
            class="blob-code-inner blob-code-marker" :data-code-marker="getCodeMarker(line.type)"
            v-html="line.code"
          />
        </td>
      </template>
      <!-- eslint-enable -->
    </template>
  </tr>
</template>

<style lang="scss"></style>
